import { createRouter, createWebHistory } from 'vue-router';
import Dashboard from '../views/Dashboard.vue'
import Login from '../views/Login.vue'
import AdminUsers from '../views/admin/AdminUsers.vue'
import AdminUserEdit from '../views/admin/AdminUserEdit.vue'
import WxUsers from '../views/user/WxUsers.vue'
import WxUserDetail from '../views/user/WxUserDetail.vue'
import BloodSugar from '../views/bloodSugar/BloodSugar.vue'
import BloodSugarDetail from '../views/bloodSugar/BloodSugarDetail.vue'
import BloodSugarStats from '../views/bloodSugar/BloodSugarStats.vue'
import Profile from '../views/Profile.vue'
import CommunityPosts from '../views/community/CommunityPosts.vue'
import CommunityPostDetail from '../views/community/CommunityPostDetail.vue'
import CommunityComments from '../views/community/CommunityComments.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('../layouts/MainLayout.vue'),
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '仪表盘', icon: 'dashboard' }
      },
      {
        path: 'admin-users',
        name: 'AdminUsers',
        component: AdminUsers,
        meta: { title: '管理员用户', icon: 'user' }
      },
      {
        path: 'admin-users/new',
        name: 'AdminUserCreate',
        component: AdminUserEdit
      },
      {
        path: 'admin-users/:id',
        name: 'AdminUserEdit',
        component: AdminUserEdit
      },
      {
        path: 'wx-users',
        name: 'WxUsers',
        component: WxUsers,
        meta: { title: '微信用户管理', icon: 'team' }
      },
      {
        path: 'wx-users/:id',
        name: 'WxUserDetail',
        component: WxUserDetail,
        meta: { title: '用户详情', hideInMenu: true }
      },
      {
        path: 'blood-sugar',
        name: 'BloodSugar',
        component: BloodSugar,
        meta: { title: '血糖记录管理', icon: 'medicine-box' }
      },
      {
        path: 'blood-sugar/:id',
        name: 'BloodSugarDetail',
        component: BloodSugarDetail,
        meta: { title: '血糖记录详情', hideInMenu: true }
      },
      {
        path: 'blood-sugar-stats',
        name: 'BloodSugarStats',
        component: BloodSugarStats,
        meta: { title: '血糖统计分析', icon: 'line-chart' }
      },
      // 饮食管理路由
      {
        path: 'diet',
        name: 'Diet',
        component: () => import('../views/diet/DietManage.vue'),
        meta: { title: '饮食管理', icon: 'coffee' }
      },
      {
        path: 'diet/foods',
        name: 'DietFoods',
        component: () => import('../views/diet/Foods.vue'),
        meta: { title: '食物管理' }
      },
      {
        path: 'diet/categories',
        name: 'DietCategories',
        component: () => import('../views/diet/Categories.vue'),
        meta: { title: '食物分类' }
      },
      {
        path: 'diet/recipes',
        name: 'DietRecipes',
        component: () => import('../views/diet/Recipes.vue'),
        meta: { title: '食谱管理' }
      },
      {
        path: 'diet/records',
        name: 'DietRecords',
        component: () => import('../views/diet/Records.vue'),
        meta: { title: '饮食记录' }
      },
      // 文章管理路由
      {
        path: 'articles',
        name: 'Articles',
        component: () => import('../views/articles/ArticleList.vue'),
        meta: { title: '文章管理', icon: 'read' }
      },
      {
        path: 'articles/new',
        name: 'ArticleCreate',
        component: () => import('../views/articles/ArticleEdit.vue'),
        meta: { title: '新增文章', hideInMenu: true }
      },
      {
        path: 'articles/:id',
        name: 'ArticleEdit',
        component: () => import('../views/articles/ArticleEdit.vue'),
        meta: { title: '编辑文章', hideInMenu: true }
      },
      {
        path: 'articles/categories',
        name: 'ArticleCategories',
        component: () => import('../views/articles/ArticleCategories.vue'),
        meta: { title: '文章分类', icon: 'tag' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: Profile,
        meta: { title: '个人设置', hideInMenu: true }
      },
      {
        path: 'community/posts',
        name: 'CommunityPosts',
        component: CommunityPosts
      },
      {
        path: 'community/posts/:id',
        name: 'CommunityPostDetail',
        component: CommunityPostDetail
      },
      {
        path: 'community/comments',
        name: 'CommunityComments',
        component: CommunityComments
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('admin_token');
  
  if (to.meta.requiresAuth && !token) {
    next('/login');
  } else {
    next();
  }
});

export default router; 